import 'package:flutter/material.dart';
import '../../model/post.dart';

/**
 * 分页显示表格数据
 */
class PaginatedDataTableDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return PaginatedDataTableSw();
  }
}

class PaginatedDataTableSw extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return PaginatedDataTableState();
  }
}

class PostDataSource extends DataTableSource {
  final List<Post> _posts = postList;

  final _selectedCount = 0;

  @override
  DataRow getRow(int index) {
    return DataRow.byIndex(
      cells: <DataCell>[
        DataCell(Text(_posts[index].title)),
        DataCell(Text(_posts[index].author)),
        DataCell(Image.network(_posts[index].imageUrl)),
      ],
      index: index,
    );
  }

  @override
  int get selectedRowCount {
    return _selectedCount;
  }

  @override
  bool get isRowCountApproximate {
    return false;
  }

  @override
  int get rowCount {
    return _posts.length;
  }
}

class PaginatedDataTableState extends State<PaginatedDataTableSw> {
  //排序列的角标
  int _sortColumnIndex = 0;

  // 升序还降序
  bool _sortAscending = false;

  final PostDataSource _postDataSource = PostDataSource();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('PaginatedDataTableDemo'),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(8.0),
        child: ListView(
          children: <Widget>[
            PaginatedDataTable(
              //每页显示个数
              rowsPerPage: 5,
              header: Text('Posts'),
              source: _postDataSource,
              //排序的列
              sortColumnIndex: _sortColumnIndex,
              //升序还是降序：true 降序；
              sortAscending: _sortAscending,
              columns: [
                DataColumn(
                  label: Text('Title'),
                  onSort: (int index, bool ascending) {
                    debugPrint("ascending.Title: $index,$ascending");
                    change(index, ascending);
                  },
                ),
                DataColumn(
                  label: Text('Author'),
                  onSort: (int index, bool ascending) {
                    debugPrint("ascending.Author: $index,$ascending");
                    change(index, ascending);
                  },
                ),
                DataColumn(
                  label: Text('Image'),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  void change(int index, bool ascending) {
    setState(() {
      _sortColumnIndex = index;
      _sortAscending = ascending;
      //按照title的长度排列
      postList.sort((a, b) {
        if (!ascending) {
          final c = a;
          a = b;
          b = c;
        }
        switch (index) {
          case 0:
            return a.title.length.compareTo(b.title.length);
            break;
          case 1:
            return a.author.length.compareTo(b.author.length);
            break;
        }
      });
    });
  }
}
